'use strict';

define(['angular', 'barcode', 'angular-ui-router', 'angular-md5', '../../../common/utils/service', 'readable-range', '../../login/login'], function (angular) {
	var module = angular.module('memberPay', ['ui.router', 'dataService', 'login', 'angular-md5']);
	module.config(['$stateProvider', function ($stateProvider) {
		$stateProvider
				.state("member.pay", {
					url: '/pay',
					views: {
						"@": {
							controller: 'MemberPayCtrl',
							templateUrl: '/mstore/member/pay/pay.html'
						}
					}
				})
	}]);

	module.controller('MemberPayCtrl', ['$rootScope', '$state', '$scope', 'dataService', '$mdDialog', 'md5', '$interval', '$timeout',
		function ($rootScope, $state, $scope, dataService, $mdDialog, md5, $interval, $timeout) {
			$rootScope.pageTitle = "微会员支付";
			$scope.input = {};
			var timerToShowTime;

			function createBar() {
				return Promise.resolve().then(function () {
					var minutes = 3;
					var seconds = 0;
					$scope.showTime = '3:00';
					return Promise.promisify(dataService.createNewCode)(sessionStorage.store, sessionStorage.memberId).then(function (data) {
						var canvas1 = document.getElementById("barcodeCanvas");
						$interval.cancel(timerToShowTime);
						$scope.verifyCode = data;
						var barcode = new bytescoutbarcode128();
						barcode.valueSet($scope.verifyCode);
						barcode.setMargins(5, 5, 5, 5);
						barcode.setBarWidth(2);
						barcode.setSize(barcode.getMinWidth(), canvas1.height);
						barcode.private_fontSize = 0;

						canvas1.width = barcode.getMinWidth();
						barcode.exportToCanvas(canvas1);
						timerToShowTime = $interval(function () {
							if (minutes == 0 && seconds == 0) {
								$scope.showTime = minutes + ':' + seconds;
							} else if (minutes > 0 && seconds == 0) {
								seconds = 59;
								minutes--;
							} else if (minutes >= 0 && seconds > 0) {
								seconds--;
								if (seconds >= 0 && seconds <= 9) {
									seconds = '0' + seconds;
								}
							}
							$scope.showTime = minutes + ':' + seconds;
						}, 1000);

					}).finally(function () {
						if (!$scope.$$destroyed) {
							$timeout(createBar, 180 * 1000);
						} else {
							$interval.cancel(timerToShowTime);
						}
					});
				});
			}


			$scope.memberBound(function () {

				dataService.getMemberPayReq(sessionStorage.store, sessionStorage.memberId, function (err, data) {
					if (err) {
						createBar();
						return;
					}
					if (!data) {
						createBar();
					}
					$scope.reqData = data;
					$scope.storeInfo = sessionStorage.storeInfo && JSON.parse(sessionStorage.storeInfo);
				});


				function getVerifyCodePayment() {
					return Promise.resolve().then(function () {
						return Promise.promisify(dataService.getVerifyCodePayment)(sessionStorage.store, sessionStorage.memberId, $scope.verifyCode).then(function (data) {
							if (!data) {
								throw data;
							}
							$scope.paymentInfo = data;
						}).catch(function (err) {
							if (!$scope.$$destroyed)
								$timeout(getVerifyCodePayment, 2000);
						});
					});
				}

				getVerifyCodePayment();


				$scope.subpage = 'pay';
				$scope.pay = function () {
					dataService.updatePayReq($scope.reqData, function (err, data) {
						if (err) {
							$scope.subpage = 'error';
							$scope.errMsg = data;
							return;
						}
						$scope.subpage = 'success';
					});
				}

			}, {force: true});
			$scope.$on('$destroy', function () {
				$interval.cancel(timerToShowTime);
			});
		}]);
});
